package com.xatu.yl.storeshopping.activity.Fragment.ShoppingInfoFragment;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.namespace.R;
import com.xatu.yl.storeshopping.activity.ShoppingInfoActivity;
import com.xatu.yl.storeshopping.vo.ShopInfo;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class OrderShoppingFragment extends Fragment implements View.OnClickListener{
    private View root;
    private ImageView shopping_icon;
    private TextView shopping_name,shopping_sale_month,shopping_price,shopping_description;
    private ImageButton reduceBtn,addBtn;
    private TextView shopping_number;
    private TextView total_money;
    private Button summaryBtn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //如果不存在，则创建
        if(root==null){
            root=inflater.inflate(R.layout.order_shopping_fragment, container, false);
        }


        shopping_icon = root.findViewById(R.id.shopping_icon);
        shopping_name = root.findViewById(R.id.shopping_name);
        shopping_sale_month = root.findViewById(R.id.shopping_sale_month);
        shopping_price = root.findViewById(R.id.shopping_price);
        shopping_description = root.findViewById(R.id.shopping_description);


        reduceBtn = root.findViewById(R.id.reduceBtn);
        addBtn = root.findViewById(R.id.addBtn);
        shopping_number = root.findViewById(R.id.shopping_number);
        total_money = root.findViewById(R.id.total_money);
        summaryBtn = root.findViewById(R.id.summaryBtn);

        addBtn.setOnClickListener(this);
        reduceBtn.setOnClickListener(this);
        summaryBtn.setOnClickListener(this);
        //获取商品信息
        getShoppingInfo();
        return root;
    }

    private void getShoppingInfo(){
        //发送请求，从web端获取
        shopping_icon.setImageBitmap(null);
        shopping_name.setText("");
        shopping_sale_month.setText("");
        shopping_price.setText("");
        shopping_description.setText("");
    }


    @Override
    public void onClick(View v) {
        //现在的商品数量
        int shoppingNumber = Integer.parseInt(shopping_number.getText().toString());
        //商品价格
        double shoppingPrice = Double.parseDouble(shopping_price.getText().toString());
        if(v.getId()==R.id.reduceBtn){
            //商品数量-1
            shopping_number.setText((shoppingNumber-1)+"");
        }
        if(v.getId()==R.id.addBtn){
            //商品数量+1
            shopping_number.setText((Integer.parseInt(shopping_number.getText().toString())+1)+"");
        }
        if(v.getId()==R.id.summaryBtn){
            if(shoppingNumber==0){
                //如果数量是0，则提示
            }else{

            }
        }

        //总价变化
        total_money.setText((shoppingNumber*shoppingPrice)+"");
    }
}


